package com.ruoyi.server.api.setting.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.server.api.setting.domain.ImgSetting;
import com.ruoyi.server.api.setting.service.IImgSettingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家设置营业时间Controller
 *
 * @author ruoyi
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/setting/setting")
public class ImgSettingController extends BaseController {
    @Autowired
    private IImgSettingService imgSettingService;

    /**
     * 查询商家设置营业时间列表
     */
//    @PreAuthorize("@ss.hasPermi('setting:setting:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImgSetting imgSetting) {
        startPage();
        List<ImgSetting> list = imgSettingService.selectImgSettingList(imgSetting);
        return getDataTable(list);
    }

    /**
     * 导出商家设置营业时间列表
     */
    @PreAuthorize("@ss.hasPermi('setting:setting:export')")
    @Log(title = "商家设置营业时间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImgSetting imgSetting) {
        List<ImgSetting> list = imgSettingService.selectImgSettingList(imgSetting);
        ExcelUtil<ImgSetting> util = new ExcelUtil<ImgSetting>(ImgSetting.class);
        util.exportExcel(response, list, "商家设置营业时间数据");
    }

    /**
     * 获取商家设置营业时间详细信息
     */
//    @PreAuthorize("@ss.hasPermi('setting:setting:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(imgSettingService.selectImgSettingById(id));
    }

    /**
     * 新增商家设置营业时间
     */
    @PreAuthorize("@ss.hasPermi('setting:setting:add')")
    @Log(title = "商家设置营业时间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImgSetting imgSetting) {
        return toAjax(imgSettingService.insertImgSetting(imgSetting));
    }

    /**
     * 修改商家设置营业时间
     */
//    @PreAuthorize("@ss.hasPermi('setting:setting:edit')")
//    @Log(title = "商家设置营业时间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImgSetting imgSetting) {
        if (imgSettingService.queryTime() == null) {
            imgSettingService.insertImgSettingTime(imgSetting);
            return success();
        } else {
            imgSetting.setId(1l);
            imgSettingService.updateImgSetting(imgSetting);
            return success();
        }
    }

    /**
     * 删除商家设置营业时间
     */
    @PreAuthorize("@ss.hasPermi('setting:setting:remove')")
    @Log(title = "商家设置营业时间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imgSettingService.deleteImgSettingByIds(ids));
    }

    /**
     * @Author Lx
     * @Description 查询营业时间
     * @Date 2024/11/21 13:35
     * @Return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("queryTime")
    public AjaxResult queryTime() {
        return success(imgSettingService.queryTime());
    }
}
